package com.whfc.ms.api.fmam.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fmam.entity.dto.FmamPlanDTO;
import com.whfc.fmam.entity.dto.FmamPlanListCountDTO;
import com.whfc.fmam.entity.qo.FmamPlanAddQO;
import com.whfc.fmam.entity.qo.FmamPlanCheckQO;
import com.whfc.fmam.entity.qo.FmamPlanEditQO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fmam.service.MatPlanService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * @author sun_guodong
 * @description 材料字典
 * @date 2019-11-18
 */
@RequiresPermissions("fmam/plan")
@RestController
@RequestMapping("/ms/api/fmam/plan")
public class FmamMatPlanController extends BaseController {

    @Autowired
    private MatPlanService matPlanService;


    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("type") Integer type,
                       @RequestParam(name = "states", required = false) String states,
                       @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                       @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        logger.info("获取材料计划列表,loginer:{}, deptId:{}, pageNum:{}, pageSize:{}, type:{}, states:{}, startDate:{}, " +
                "endDate:{}", user.getUsername(), deptId, pageNum, pageSize, type, states, startDate, endDate);
        PageData<FmamPlanDTO> listData = matPlanService.matPlanList(user, deptId, pageNum, pageSize, type, states,
                startDate, endDate);
        return ResultUtil.success(listData);
    }

    @GetMapping("/list/count")
    public Result listCount(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("deptId") Integer deptId,
                            @RequestParam(name = "states", required = false) String states,
                            @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                            @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        logger.info("获取材料计划列表数量,loginer:{}, deptId:{}, states:{}, startDate:{}, endDate:{}",
                user.getUsername(), deptId, states, startDate, endDate);
        FmamPlanListCountDTO planListCount = matPlanService.matPlanListCount(user, deptId, states, startDate, endDate);
        return ResultUtil.success(planListCount);
    }


    /**
     * 获取材料计划详情
     *
     * @param user
     * @param planId
     * @return
     */
    @GetMapping("/detail")
    public Result matPlanDetail(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam Integer planId) {
        logger.info("获取材料计划详情,loginer:{},planId:{}", user.getUsername(), planId);
        FmamPlanDTO fmamPlanDTO = matPlanService.matPlanDetail(user, planId);
        return ResultUtil.success(fmamPlanDTO);
    }

    /**
     * 删除材料计划
     *
     * @param user
     * @param planId 计划ID
     * @return
     */
    @PostMapping("/del/{planId}")
    public Result matPlanDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable Integer planId) {
        logger.info("删除材料计划,loginer:{},planId:{}", user.getUsername(), planId);
        matPlanService.delMatPlan(planId);
        return ResultUtil.success();
    }

    /**
     * 新增并提交材料计划
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("fmam/plan/add")
    @PostMapping("/add")
    public Result matPlanAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody FmamPlanAddQO request) {
        logger.info("新增材料计划,loginer:{},request:{}", user.getUsername(), request.toString());
        matPlanService.addMatPlan(user, request);
        return ResultUtil.success();
    }

    /**
     * 重新填报被驳回的审批计划
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/edit")
    public Result matPlanEdit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody FmamPlanEditQO request) {
        logger.info(" 重新填报材料审批计划,loginer:{},FmamPlanEditQO:{}", user.getUsername(), request.toString());
        matPlanService.editMatPlan(user, request);
        return ResultUtil.success();
    }

    /**
     * 材料计划审批
     *
     * @param user
     * @return
     */
    @Deprecated
    @PostMapping("/check")
    public Result matPlanCheck(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody FmamPlanCheckQO request) {
        logger.info("材料计划审批,loginer:{},request:{}", user.getUsername(), request.toString());
        matPlanService.checkMatPlan(user, request);
        return ResultUtil.success();
    }

    /**
     * 材料计划审批
     *
     * @param user
     * @return
     */
    @RequiresPermissions("fmam/plan/check")
    @PostMapping("/check1")
    public Result matPlanCheck1(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody FmamPlanCheckQO request) {
        logger.info("材料计划审批(一审),loginer:{},request:{}", user.getUsername(), request.toString());
        matPlanService.check1(user, request);
        return ResultUtil.success();
    }

    /**
     * 材料计划复审
     *
     * @param user
     * @return
     */
    @RequiresPermissions("fmam/plan/review")
    @PostMapping("/check2")
    public Result matPlanCheck2(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody FmamPlanCheckQO request) {
        logger.info("材料计划复审(二审),loginer:{},request:{}", user.getUsername(), request.toString());
        matPlanService.check2(user, request);
        return ResultUtil.success();
    }

    /**
     * 材料计划核验
     *
     * @param user
     * @return
     */
    @RequiresPermissions("fmam/plan/verify")
    @PostMapping("/check3")
    public Result matPlanCheck3(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody FmamPlanCheckQO request) {
        logger.info("材料计划核验(三审),loginer:{},request:{}", user.getUsername(), request.toString());
        matPlanService.check3(user, request);
        return ResultUtil.success();
    }


    /**
     * 材料附件上传
     */
    @PostMapping("/file/upload")
    public Result uploadFile(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam(value = "file") MultipartFile file) throws IOException {
        logger.info("材料附件上传,loginer:{},file:{}", user.getUsername(), file.toString());
        OssPathDTO ossPathDTO = matPlanService.uploadFile(file);
        return ResultUtil.success(ossPathDTO);
    }


}
